<template>
    <div>
         <!--导航区域-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>基础数据</el-breadcrumb-item>
        <el-breadcrumb-item>数据字典</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card>
          <el-row :gutter="25">
              <el-col :span="5">
                  <el-input size="small" clearable placeholder="输入类型" v-model="queryParams.type" /> 
              </el-col>
              <el-col :span="5">
                  <el-input size="small" clearable placeholder="输入描述" v-model="queryParams.keyDesc" /> 
              </el-col>
              <el-col :span="1" style="margin-left:5px">
                  <el-button size="small" type="danger" @click="clear" icon="el-icon-delete">清空</el-button>
              </el-col>
              <el-col :span="1" style="margin-left:28px">
                  <el-button size="small" type="success" @click="queryPage" icon="el-icon-search" v-if="permissionStrValue.indexOf('dataDictionary:query') !== -1">查询</el-button>
              </el-col>
              <el-col :span="1" style="margin-left:28px">
                  <el-button size="small" type="primary" @click="add" icon="el-icon-plus" v-if="permissionStrValue.indexOf('dataDictionary:add') !== -1">新增</el-button>
              </el-col>
          </el-row>

          <!--放表格-->
        <el-table :data="list" border stripe height="500">
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column label="数据类型">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.type }}</span>
                </template>
            </el-table-column>
            <el-table-column label="数据描述">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.keyDesc }}</span>
                </template>
            </el-table-column>
            <el-table-column label="数据值">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.value }}</span>
                </template>
            </el-table-column>
            <el-table-column label="排序字段">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.sort}}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.createTime}}</span>
                </template>
            </el-table-column>
            <el-table-column label="修改时间">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.modifyTime}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="250px">
                 <template slot-scope="scope">
                     <el-button type="primary" size="small" @click="updateById(scope.row.id)" icon="el-icon-edit" v-if="permissionStrValue.indexOf('dataDictionary:update') !== -1">编辑</el-button>
                     <el-button type="danger" size="small" @click="removeById(scope.row.id)" icon="el-icon-delete" v-if="permissionStrValue.indexOf('dataDictionary:delete') !== -1">删除</el-button>
                 </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.currPage"
          :page-sizes="[10,20, 30, 40, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

      </el-card>


      <el-dialog
       title="新增数据"
       :visible.sync="addFlag"
       width="30%"
       @close="clearForm()">
       <el-form :model="baseData" label-width="80px" ref="baseDataForm" label-position="left" :rules="baseDataRules">
         <el-form-item label="类型:" prop="type">
           <el-input v-model="baseData.type" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="描述:" prop="keyDesc">
           <el-input v-model="baseData.keyDesc" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="key值:" prop="value">
           <el-input v-model="baseData.value" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="排序值:" prop="sort">
           <el-input v-model="baseData.sort" size="small" ></el-input>
         </el-form-item>
       </el-form>
       <span slot="footer" class="dialog-footer">
          <el-button @click="addFlag = false">取 消</el-button>
          <el-button type="primary" @click="addBaseData()">确 定</el-button>
       </span>
     </el-dialog>

     <el-dialog title="编辑" :visible.sync="settingDialogVisible" width="30%" @close="clearForm()">
        <el-form :model="baseData" label-width="80px" :rules="baseDataRules" >
          <el-form-item label="类型:" prop="type">
            <el-input v-model="baseData.type" size="small" ></el-input>
          </el-form-item>
          <el-form-item label="描述:" prop="keyDesc">
            <el-input v-model="baseData.keyDesc" size="small" ></el-input>
          </el-form-item>
          <el-form-item label="key值:" prop="value">
            <el-input v-model="baseData.value" size="small" ></el-input>
          </el-form-item>
          <el-form-item label="排序:" prop="value">
            <el-input v-model="baseData.sort" size="small" ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="settingDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="update()" >修改</el-button>
      </span>
      </el-dialog>

        

    </div>
</template>

<script>
export default {
    name: 'DataDictionary',
    
    data() {
        return {
            queryParams: {
                type: '',
                keyDesc: '',
                currPage: 1,
                pageSize: 10
            },
            list: [],
            total: 0,
            baseData: {
                id: '',
                type: '',
                keyDesc: '',
                value: -1,
                sort: -1
            },
            addFlag: false,
            settingDialogVisible: false,
            permissionStrValue: '',
            baseDataRules: {
               type: [
                    { required: true, message: '请输入类型', trigger: 'blur' }
               ],
               keyDesc: [
                   { required: true, message: '请输入描述', trigger: 'blur' }
               ],
               value: [
                   { required: true, message: '请输入key值', trigger: 'blur' }
               ],
               sort: [
                   { required: true, message: '请输入排序值', trigger: 'blur' }
               ]
            }

        }
    },
    created() {
        this.queryPage();
        //获取权限值
       this.permissionStrValue = JSON.parse(window.sessionStorage.getItem('permissionList')).toString();
    },
    methods: {
        //分页查询
        async queryPage() {
            const {data: res} = await this.$http.post('/base-data/list-page',JSON.stringify(this.queryParams), {
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                }
        });
           console.log(res)
            if(res.success) {
                this.list = res.data;
                this.total = res.total;
            } else {
                this.$message.error(res.message);
            }

        },
        //删除
        async removeById(id) {
            const confirmResult = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => {
                return err;
            });
            //当用户确认时，则返回值为字符串：confirm
            //取消时，返回字符串cancel
            if (confirmResult !== 'confirm'){
                return this.$message.info("已取消删除")
            }
          
            const{data: res} = await this.$http.delete('/base-data/delete/' + id);
            if(res.success) {
                this.$message.success("删除成功！");
                this.init();
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }
        },
        //清空
        clear() {
            this.queryParams.type = '';
            this.queryParams.keyDesc = '';
        },
        init() {
            this.queryParams.type = '';
            this.queryParams.keyDesc = '';
            this.queryParams.currPage = 1;
            this.queryParams.pageSize = 10;

        },
        //新增
        add() {
            this.addFlag = true;

        },
        handleSizeChange(newPageSize) {
            this.queryParams.pageSize = newPageSize;
            this.queryPage();
        },
        handleCurrentChange(newPage) {
            this.queryParams.currPage = newPage;
            this.queryPage();

        },
        clearForm() {
            this.baseData.type = '';
            this.baseData.keyDesc = '';
            this.baseData.value = -1;
            this.baseData.sort = -1;

        },
        addBaseData() {
            this.$refs.baseDataForm.validate(async valid => {
                if(valid) {
                    const {data: res} = await this.$http.post('/base-data/add-base-data', JSON.stringify(this.baseData), {
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8',
                        }
                    })
            
                    if(res.success) {
                        this.$message.success("增加成功！");
                        this.addFlag = false;
                        this.init();
                        this.queryPage();
                    } else{
                        this.$message.error(res.message);
                    }
                }
            })
            
        },
        async updateById(id) {
            this.settingDialogVisible = true;
            const{data: res} = await this.$http.get('/base-data/get-by-id/' + id);
            if(!res.success) {
                return this.$message.error(res.message);
            }
            this.baseData = res.data.data;
        },
        async update() {
            const{data: res} = await this.$http.put('/base-data/update', JSON.stringify(this.baseData), {
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                }
            });
            if(res.success) {
                this.$message.success("修改成功！")
                this.settingDialogVisible = false;
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }
        }

    }
    
}
</script>

<style >

</style>